#include "rknn_yolovX.h"
#include <QApplication>
#include <QtDebug>
#include <QThread>
#include <chrono>
#include <unistd.h>
#include "threads.h"
#include "V4L2CamCTL.h"
#include <iostream>
char* IMG_NAME=(char*)"bus.jpg";//模型相对路径
RKNN_YOLOVX::RKNN_YOLOVX(MainWindow *mainWindow,int cameras,std::string &model_name,const char* label_path):m_mainWindow(mainWindow)
{
    int threadNum = 1;
    testPool = new rknnPool<rkYolov8_seg, cv::Mat, cv::Mat>(model_name, threadNum);
    if (testPool->init() != 0){
        qDebug()<<"init rknnPool fail";
        delete this;
    }
    // rkYolov8_seg_obj = new rkYolov5s(model_name);

    // rkYolov8_seg_obj->init(rkYolov8_seg_obj->get_pctx(),0);

    // std::cout << "sleep" + std::to_string(k) << std::endl;

}
RKNN_YOLOVX::~RKNN_YOLOVX(){
    if (testPool!=nullptr)delete testPool;
    
    qDebug()<<"delete RKNN_YOLOVX";
    #ifdef USE_RKNNPOOL
    delete ptr;
    #endif
    // rknn_outputs_release(app_ctx->rknn_ctx, app_ctx->io_num.n_output, outputs);
    // delete semaphore;
}

#include <qthread.h>
void RKNN_YOLOVX::run_bypass()
{
}
cv::Mat rgbBufferToMat(const unsigned char* data, int width, int height)
{
    // 创建一个具有正确尺寸和类型的 Mat 对象
    cv::Mat rgb(height, width, CV_8UC3, const_cast<unsigned char*>(data));
    cv::Mat bgr;
    cv::cvtColor(rgb, bgr, cv::COLOR_RGB2BGR);
    return bgr;
}
extern QSemaphore semaphore;
cv::Mat rotatedImage;

void RKNN_YOLOVX::run(cv::Mat &imageData)
{
    // std::this_thread::sleep_for(std::chrono::milliseconds(50));

    auto total_start = std::chrono::high_resolution_clock::now();

    // printf("handleDataFromL4V2Thread\n");
    cv::flip(imageData, rotatedImage, -1);

    

    // cv::imshow("Camera FPS", rotatedImage);
    cv::Mat img;
    rotatedImage.copyTo(img);
    
    // goto endLoops;

    // cv::imshow("Camera source", imageData);
    if (testPool->put(img) != 0)
    {
        qDebug() << "put image to rknnPool not success";
    }
    
    if (testPool->get(img) != 0)
    {
        qDebug() << "get image from rknnPool not success";
    }
    
endLoops:
    cv::Mat output_mat;
    cvtColor(img, output_mat, CV_BGR2RGB);
    QImage qimage(output_mat.data,
                output_mat.cols,
                output_mat.rows,
                output_mat.step,
                QImage::Format_RGB888);
    QMetaObject::invokeMethod(m_mainWindow, "updateUI1", Qt::QueuedConnection, Q_ARG(QPixmap, QPixmap::fromImage(qimage)));

    // semaphore.release();

    auto total_end = std::chrono::high_resolution_clock::now();

    // qDebug() << "RKNN_YOLOVX::run:" << std::chrono::duration_cast<std::chrono::microseconds>(total_end - total_start).count()/1000.0 << "ms";

}

extern QMutex mutex;
void RKNN_YOLOVX::handleDataFromL4V2Thread(cv::Mat &imageData) {
    // printf("handleDataFromL4V2Thread\n");
    // rkYolov8_seg_obj->infer(imageData);
    cv::imshow("Camera FPS", imageData);
    semaphore.release();
}
void RKNN_YOLOVX::statusCtl(bool status)
{
    mutex.lock();
    enable=status;
    qDebug() << "statusCtl:" <<status;
    mutex.unlock();
}
int RKNN_YOLOVX::get_ObjectId()
{
    mutex.lock();
    auto value = _obj_id;
    mutex.unlock();
    return value;

}
void RKNN_YOLOVX::set_ObjectId(int set_ObjectId)
{
    mutex.lock();
    _obj_id=set_ObjectId;
    mutex.unlock();
}
bool RKNN_YOLOVX::get_detected()
{
    mutex.lock();
    auto value = had_detected;
    mutex.unlock();
    return value;
}
void RKNN_YOLOVX::set_detected(bool result)
{
    mutex.lock();
    had_detected = result;
    mutex.unlock();
}
void RKNN_YOLOVX::set_classify(bool result)
{
    mutex.lock();
    multy_classify = result;
    mutex.unlock();
}
bool RKNN_YOLOVX::get_classify()
{
    mutex.lock();
    auto value = multy_classify;
    mutex.unlock();
    return value;

}
